﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using ResxHelper.Logic;
using ResxHelper.Logic.Converter;

namespace ResxHelper.Forms
{
    public partial class FrmDropConvert : Form
    {
        private Core.ZipResourceContent _zipContent;

        public FrmDropConvert()
        {
            InitializeComponent();
            lblFileName.Text = string.Empty;

            rdoAndroidResource.Checked = true;
            _zipContent = Core.ZipResourceContent.Android;
        }

        private void pnlDropZone_DragDrop(object sender, DragEventArgs e)
        {
            var files = (string[]) e.Data.GetData(DataFormats.FileDrop);
            var file = files.FirstOrDefault();

            DialogResult result;
            var saveZipTo = DialogHelper.GetSaveFileLocation("Excel Zip File|*.zip", out result);
            if(result != DialogResult.OK) return;

            try
            {
                // import or export
                if (rdoExport.Checked)
                {
                    ExportBulk(file, saveZipTo);
                }
                else
                {
                    ImportBulk(file, saveZipTo);
                }

                Process.Start("explorer.exe", string.Format("/select, {0}", saveZipTo));

                if (chkOpenZipWhenDone.Checked)
                    Process.Start(saveZipTo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Could not read contents of the zip file: {0}", ex.Message));
            }
        }

        private void ExportBulk(string inFile, string outFile)
        {
            var converter = BulkConverterFactory.GetConverter(_zipContent);
            converter.ToExcel(inFile, outFile);
        }

        private void ImportBulk(string inFile, string outFile)
        {
            var converter = BulkConverterFactory.GetConverter(_zipContent);
            converter.ToResource(inFile, outFile);
        }

        private void pnlDropZone_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
            var files = (string[])e.Data.GetData(DataFormats.FileDrop);
            var file = files.FirstOrDefault();
            lblFileName.Text = Path.GetFileName(file);
        }

        private void pnlDropZone_DragLeave(object sender, EventArgs e)
        {
            lblFileName.Text = string.Empty;
        }

        public enum DropMode
        {
            Export,
            Import
        }

        private void rdoAndroidResource_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoAndroidResource.Checked)
            {
                _zipContent = Core.ZipResourceContent.Android;
            }
            if (rdoIOSResources.Checked)
            {
                _zipContent = Core.ZipResourceContent.IOS;
            }
            if (rdoDotNetResources.Checked)
            {
                _zipContent = Core.ZipResourceContent.DotNet;
            }
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            Hide();
            Close();
        }

        
    }
}
